<?php

namespace App\Http\Controllers;

use App\Http\Controllers\Api\CjsonController;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Foundation\Auth\Access\AuthorizesResources;
use App\Model\Cate;

class Controller extends BaseController
{
    use AuthorizesRequests, AuthorizesResources, DispatchesJobs, ValidatesRequests;
    /**
     * @return array
     * 获取分类信息
     */
    public function getCateInfo(){
        $cate = new Cate();
        $cateData = $cate->get();
        $res = $this->makeCateData($cateData,1);
        return $res;
    }
    /**
     * 递归获取
     * 无限分类
     */
    private function makeCateData($cate,$pid,$count=0){
        static $data = array();
        $count += 2;
        foreach ($cate as $k => $v){
            if($v['pid'] == $pid){
                $v->name = str_repeat('&nbsp;',$count).'|-'.$v->name;
                $data[] = $v;
                $this->makeCateData($cate,$v['id'],$count);
            }
        }
        return $data;
    }

    public function success($msg="操作成功！"){
        $origin = isset($_SERVER['HTTP_ORIGIN'])? $_SERVER['HTTP_ORIGIN'] : '';
        header('Content-Type:application/json; charset=utf-8');
        header("Access-Control-Allow-Origin:". $origin);
        header("Access-Control-Allow-Credentials: true ");
        echo json_encode(array("status"=>1,"info"=>$msg));
        exit();
    }

    public function error($msg="操作失败！"){
        $origin = isset($_SERVER['HTTP_ORIGIN'])? $_SERVER['HTTP_ORIGIN'] : '';
        header('Content-Type:application/json; charset=utf-8');
        header("Access-Control-Allow-Origin:". $origin);
        header("Access-Control-Allow-Credentials: true ");
        echo json_encode(array("status"=>0,"info"=>$msg));
        exit();
    }
}
